import { createApp,Directive } from 'vue'
import './style.css'
import App from './App.vue'
// import { createPinia } from 'pinia'
import 'babel-polyfill'
// import 'default-passive-events'
import { useMain,store } from "@/store/index";

import router from "./routes/index";
import * as directives from "@/directive/premission";
import { capturePassive } from "@/utils/polyfill";// 去除touch事件谷歌提示
import print from 'vue3-print-nb'

import vue3TreeOrg from 'vue3-tree-org';
import "vue3-tree-org/lib/vue3-tree-org.css";

capturePassive();

const app:any= createApp(App)
// 创建 pinia 实例
// const pinia = createPinia()

app.use(router);
app.use(store);
app.use(print);
app.use(vue3TreeOrg);
app.mount('#app');

//挂载自定义指令
Object.keys(directives).forEach(key => {
    app.directive(key, (directives as { [key: string]: Directive })[key]);
});

const useMainStore = useMain(store);

//路由守卫
router.beforeEach((to,from,next)=>{
    let token = useMainStore.authToken ? useMainStore.authToken : '';
   //如果是登录页,获取忘记密码页面,直接跳转
    if(to.path=='/login' || to.path=='/forgetPassword'){
        next();
    }else{
        // console.log('41',to);
        //有token直接跳转,无token则跳转回登录页
        if(token){
            next(); 
        }else{
            if(to.path=='/home' && to.query && to.query.token){
                next(); 
            }else{
                next('/login'); 
            }
            // next();
        }
    }
})

export default app

